#ifndef DDSDEFINE_H
#define DDSDEFINE_H
#include "csearchive.h"
//序列化接口
#define SERIALIZE(PARENT, RESERVED, ...) \
    INTERNAL_SERIALIZATION_IMP(PARENT,RESERVED,__VA_ARGS__)\
    INTERNAL_UNSERIALIZATION_IMP(PARENT,RESERVED,__VA_ARGS__)\
    INTERNAL_UNSERIALIZATION_IMP1(PARENT,RESERVED,__VA_ARGS__)\
    INTERNAL_THIS(RESERVED)
#define CSeArchive CSeArchive
//连接器
#define SYMBOL_CATENATE(arg1, arg2) arg1 ## arg2
#define CHARIZE_WITH_MACRO(arg) CHARIZE(arg)
#define SYMBOL_CATENATE_WITH_MACRO(arg1, arg2) SYMBOL_CATENATE(arg1, arg2)

//数轴填充(得到参数列表参数个数)默认30个，可手动扩充
#define ATTR(args) args
#define COUNT_PARMS_IMP(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30,NUM, ...) NUM
#define COUNT_PARMS(...) \
ATTR(COUNT_PARMS_IMP(__VA_ARGS__,30 , 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
//向缓存中写入数据
#define WR_ARG_1(arg) iSeArchive<<arg;
#define WR_ARG_2(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_1(__VA_ARGS__))
#define WR_ARG_3(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_2(__VA_ARGS__))
#define WR_ARG_4(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_3(__VA_ARGS__))
#define WR_ARG_5(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_4(__VA_ARGS__))
#define WR_ARG_6(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_5(__VA_ARGS__))
#define WR_ARG_7(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_6(__VA_ARGS__))
#define WR_ARG_8(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_7(__VA_ARGS__))
#define WR_ARG_9(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_8(__VA_ARGS__))
#define WR_ARG_10(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_9(__VA_ARGS__))
#define WR_ARG_11(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_10(__VA_ARGS__))
#define WR_ARG_12(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_11(__VA_ARGS__))
#define WR_ARG_13(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_12(__VA_ARGS__))
#define WR_ARG_14(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_13(__VA_ARGS__))
#define WR_ARG_15(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_14(__VA_ARGS__))
#define WR_ARG_16(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_15(__VA_ARGS__))
#define WR_ARG_17(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_16(__VA_ARGS__))
#define WR_ARG_18(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_17(__VA_ARGS__))
#define WR_ARG_19(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_18(__VA_ARGS__))
#define WR_ARG_20(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_19(__VA_ARGS__))
#define WR_ARG_21(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_20(__VA_ARGS__))
#define WR_ARG_22(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_21(__VA_ARGS__))
#define WR_ARG_23(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_22(__VA_ARGS__))
#define WR_ARG_24(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_23(__VA_ARGS__))
#define WR_ARG_25(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_24(__VA_ARGS__))
#define WR_ARG_26(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_25(__VA_ARGS__))
#define WR_ARG_27(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_26(__VA_ARGS__))
#define WR_ARG_28(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_27(__VA_ARGS__))
#define WR_ARG_29(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_28(__VA_ARGS__))
#define WR_ARG_30(arg, ...) WR_ARG_1(arg) ATTR(WR_ARG_29(__VA_ARGS__))
#define WR_ARGS(...) \
ATTR(SYMBOL_CATENATE_WITH_MACRO(WR_ARG_, ATTR(COUNT_PARMS(__VA_ARGS__)))(__VA_ARGS__))

//向缓存中读出数据
#define RD_ARG_1(arg) iSeArchive>>arg;
#define RD_ARG_2(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_1(__VA_ARGS__))
#define RD_ARG_3(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_2(__VA_ARGS__))
#define RD_ARG_4(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_3(__VA_ARGS__))
#define RD_ARG_5(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_4(__VA_ARGS__))
#define RD_ARG_6(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_5(__VA_ARGS__))
#define RD_ARG_7(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_6(__VA_ARGS__))
#define RD_ARG_8(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_7(__VA_ARGS__))
#define RD_ARG_9(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_8(__VA_ARGS__))
#define RD_ARG_10(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_9(__VA_ARGS__))
#define RD_ARG_11(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_10(__VA_ARGS__))
#define RD_ARG_12(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_11(__VA_ARGS__))
#define RD_ARG_13(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_12(__VA_ARGS__))
#define RD_ARG_14(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_13(__VA_ARGS__))
#define RD_ARG_15(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_14(__VA_ARGS__))
#define RD_ARG_16(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_15(__VA_ARGS__))
#define RD_ARG_17(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_16(__VA_ARGS__))
#define RD_ARG_18(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_17(__VA_ARGS__))
#define RD_ARG_19(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_18(__VA_ARGS__))
#define RD_ARG_20(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_19(__VA_ARGS__))
#define RD_ARG_21(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_20(__VA_ARGS__))
#define RD_ARG_22(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_21(__VA_ARGS__))
#define RD_ARG_23(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_22(__VA_ARGS__))
#define RD_ARG_24(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_23(__VA_ARGS__))
#define RD_ARG_25(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_24(__VA_ARGS__))
#define RD_ARG_26(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_25(__VA_ARGS__))
#define RD_ARG_27(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_26(__VA_ARGS__))
#define RD_ARG_28(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_27(__VA_ARGS__))
#define RD_ARG_29(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_28(__VA_ARGS__))
#define RD_ARG_30(arg, ...) RD_ARG_1(arg) ATTR(RD_ARG_29(__VA_ARGS__))
#define RD_ARGS(...) \
ATTR(SYMBOL_CATENATE_WITH_MACRO(RD_ARG_, ATTR(COUNT_PARMS(__VA_ARGS__)))(__VA_ARGS__))

//序列化
#define INTERNAL_SERIALIZATION_IMP(PARENT,RESERVED, ...)\
virtual std::string Serialize(){\
    CSeArchive iSeArchive;\
    std::string ret;\
if(#PARENT != #RESERVED)\
    {\
        std::string sParent = PARENT::Serialize();\
        iSeArchive.writeMemory(sParent.data(),sParent.size());\
    }\
    WR_ARGS(__VA_ARGS__);\
    iSeArchive.get_buf(ret);\
    return ret;}
//反序列化
#define INTERNAL_UNSERIALIZATION_IMP(PARENT,RESERVED, ...)\
virtual bool UnSerialize(std::string sInData){\
    CSeArchive iSeArchive(sInData.c_str(), sInData.length());\
    if(#PARENT != #RESERVED)\
    {\
        PARENT::UnSerialize(sInData);\
        iSeArchive.bufPtrMove((PARENT::Serialize()).size());\
    }\
    RD_ARGS(__VA_ARGS__);\
    return true;}
//反序列化（其他类调用）
#define INTERNAL_UNSERIALIZATION_IMP1(PARENT,RESERVED, ...)\
virtual bool UnSerialize(CSeArchive &iSeArchive){\
    if(#PARENT != #RESERVED)\
    {\
        PARENT::UnSerialize(iSeArchive);\
    }\
    RD_ARGS(__VA_ARGS__);\
    return true;}

#define INTERNAL_THIS(RESERVED)\
protected:\
    std::string RESERVED##Serialize()\
    {\
        return RESERVED::Serialize();\
    }\
    bool RESERVED##UnSerialize(std::string sInData)\
    {\
        return RESERVED::UnSerialize(sInData);\
    }\

#endif // DDSDEFINE_H
